import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/models/response/message_like_collect_response.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:sainbainu/common/widgets/widget.dart';

import 'index.dart';

class LikeCollectPage extends StatelessWidget {
  const LikeCollectPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LikeCollectController>(
      init: LikeCollectController(),
      builder: (_) {
        return Scaffold(
          body: EasyRefresh.builder(
            controller: _.state.refreshController,
            onRefresh: _.onRefresh,
            onLoad: _.onRichBottom,
            childBuilder: (context, physics) {
              return _buildBody(physics);
            },
          ),
        );
      },
    );
  }

  Widget _buildBody(ScrollPhysics physics) {
    return GetBuilder<LikeCollectController>(
      id: 'like_collec',
      builder: (_) => ListView.separated(
        physics: physics,
        itemCount: _.state.list.rows.length,
        separatorBuilder: (BuildContext context, int index) {
          return const SizedBox(height: 1);
        },
        itemBuilder: (BuildContext context, int index) {
          MessageLikeCollectResponse item = _.state.list.rows[index];
          return Container(
            color: Colors.white,
            padding: const EdgeInsets.symmetric(
              vertical: 10,
              horizontal: 15,
            ),
            child: UserInfoCell(
              name: item.customerName ?? '',
              avatar: item.avatar,
              role: item.operateMsg,
              roleFontSize: 13,
              showNameAuth: item.officialFlag == 1,
              rightChild: GestureDetector(
                onTap: () => _.toContentDetailPage(item.noteId),
                child: _buildContent(item),
              ),
              onUserTap: () => _.toUserDetailPage(item.customerId),
            ),
          );
        },
      ),
    );
  }

  Widget _buildContent(MessageLikeCollectResponse item) {
    if (item.text != null && item.text!.isNotEmpty) {
      return SizedBox(
        width: 100,
        child: Text(
          item.text!,
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
          textAlign: TextAlign.end,
          style: const TextStyle(color: AppColors.unactive3, fontSize: 12),
        ),
      );
    } else if (item.image != null && item.image!.isNotEmpty) {
      return SizedBox(
        width: 50,
        height: 50,
        child: ClipRRect(
          borderRadius: BorderRadius.circular(6),
          child: CustomImage(url: item.image!),
        ),
      );
    } else if (item.video != null && item.video!.isNotEmpty) {
      return const Text(
        '[视频内容]',
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        textAlign: TextAlign.end,
        style: TextStyle(color: AppColors.unactive3, fontSize: 12),
      );
    } else if (item.audio != null && item.audio!.isNotEmpty) {
      return const Text(
        '[语音内容]',
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        textAlign: TextAlign.end,
        style: TextStyle(color: AppColors.unactive3, fontSize: 12),
      );
    } else {
      return const SizedBox.shrink();
    }
  }
}
